//获取app对象
const app = getApp()
//引入api接口
const { login } = require('../../utils/api')
const { returnBack } = require('../../utils/util')
//头像地址
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
	data: {
		visible: false,
		//用户信息
		userInfo: {
			avatarUrl: defaultAvatarUrl,
			nickName: '',
			tel: '18744822723',
			password: '123456'
		},
		hasUserInfo: false,
		canIUseGetUserProfile: wx.canIUse('getUserProfile'),
		canIUseNicknameComp: wx.canIUse('input.type.nickname'),
	},
	//点击登录弹出选项
	handlePopup(e) {
		this.setData({
			visible: true
		});
	},
	onVisibleChange(e) {
		this.setData({
			visible: e.detail.visible,
		});
	},
	//登录
	userLogin(e) {
		console.log(e.target.dataset.type);
		const type = e.target.dataset.type
		const { tel } = this.data.userInfo
		const { password } = this.data.userInfo
		if (tel == '') {
			wx.showToast({
				title: '请输入手机号',
				icon: 'error'
			})
		} else if (password == '') {
			wx.showToast({
				title: '请输入密码',
				icon: 'error'
			})
		} else {
			//发请求去后端查用户
			login(tel, password, type).then((res) => {
				//如果请求成功，把返回的用户放到app里的currentUser中，重定向到主页
				if (res.code == 200) {
					app.globalData.currentUser = res.data
					console.log("当前登录用户为", app.globalData.currentUser);
					returnBack('/pages/hospital/users', '登陆成功')
				} else {
					this.setData({
						hasUserInfo: false
					})
				}
			})
		}

	},
	//跳转注册
	toRegister() {
		wx.redirectTo({
		  url: '/pages/register/register',
		})
	},

	//跳转到日志页面
	bindViewTap() {
		wx.navigateTo({
			url: '../logs/logs'
		})
	},
	//选择头像
	onChooseAvatar(e) {
		const avatarUrl = e.detail.avatarUrl
		const {
			nickName
		} = this.data.userInfo
		const {
			tel
		} = this.data.userInfo
		const {
			password
		} = this.data.userInfo
		this.setData({
			"userInfo.avatarUrl": avatarUrl,
			hasUserInfo: tel && password && nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
		})
	},
	//填写昵称
	changeNickname(e) {
		const nickName = e.detail.value
		const {
			avatarUrl
		} = this.data.userInfo
		const {
			tel
		} = this.data.userInfo
		const {
			password
		} = this.data.userInfo
		this.setData({
			"userInfo.nickName": nickName,
			hasUserInfo: tel && password && nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
		})
	},
	//填写手机号
	changeTel(e) {
		const tel = e.detail.value
		const {
			nickName
		} = this.data.userInfo
		const {
			password
		} = this.data.userInfo
		const {
			avatarUrl
		} = this.data.userInfo
		this.setData({
			"userInfo.tel": tel,
			hasUserInfo: tel && password && nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
		})
	},
	//填写密码
	changePassword(e) {
		const password = e.detail.value
		const {
			nickName
		} = this.data.userInfo
		const {
			tel
		} = this.data.userInfo
		const {
			avatarUrl
		} = this.data.userInfo
		this.setData({
			"userInfo.password": password,
			hasUserInfo: tel && password && nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
		})
	},
	//获取用户信息
	getUserProfile(e) {
		// 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
		wx.getUserProfile({
			desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			success: (res) => {
				console.log(res)
				this.setData({
					userInfo: res.userInfo,
					hasUserInfo: true
				})
			}
		})
	},
})